package com.huirui.server.controller.mini.program;

import com.huirui.server.context.UserContext;
import com.huirui.server.pojo.dto.DeliveryAddressDTO;
import com.huirui.server.pojo.entity.DeliveryAddress;
import com.huirui.server.pojo.result.Result;
import com.huirui.server.pojo.vo.DeliveryAddressVo;
import com.huirui.server.service.DeliveryAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/delivery/address")
@Slf4j
@Validated
@Tag(name = "用户收货地址管理", description = "微信小程序用户收获地址相关")
public class DeliveryAddressController {
    @Resource
    private DeliveryAddressService deliveryAddressService;

    @GetMapping
    @Operation(summary = "查询用户所有的收货地址")
    public Result<List<DeliveryAddressVo>> listUserAddress() {
        List<DeliveryAddressVo> list = deliveryAddressService.listUserAddress();
        return Result.success(list);
    }

    @PostMapping
    @Operation(summary = "用户新增收货地址")
    public Result<Void> addDeliveryAddress(@Valid @RequestBody DeliveryAddressDTO addressDTO) {
        deliveryAddressService.save(addressDTO);
        return Result.success();
    }

    @PatchMapping("/{id}")
    @Operation(summary = "修改默认操作地址")
    public Result<Void> setDefaultAddress(@PathVariable Long id) {
        deliveryAddressService
                .lambdaUpdate()
                .eq(DeliveryAddress::getUserId, UserContext.getId())
                .set(DeliveryAddress::getIsDefault, 0)
                .update();

        deliveryAddressService
                .lambdaUpdate()
                .eq(DeliveryAddress::getId, id)
                .set(DeliveryAddress::getIsDefault, 1)
                .update();
        return Result.success();
    }

    @GetMapping("/count")
    @Operation(summary = "查询收货地址数量")
    public Result<Long> getDeliveryAddressCount() {
        Long count = deliveryAddressService
                .lambdaQuery()
                .eq(DeliveryAddress::getUserId, UserContext.getId())
                .count();
        return Result.success(count);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "根据收获地址ID删除收获地址")
    public Result<Void> deleteDeliveryAddressById(@PathVariable Long id) {
        deliveryAddressService.deleteDeliveryAddressById(id);
        return Result.success();
    }

    @PutMapping("/{id}")
    @Operation(summary = "用户修改收货地址")
    public Result<Void> modifyDeliveryAddress(@PathVariable Long id, @Valid @RequestBody DeliveryAddressDTO addressDTO) {
        deliveryAddressService.modifyDeliveryAddress(id, addressDTO);
        return Result.success();
    }

    @GetMapping("/default")
    @Operation(summary = "查询默认收货地址")
    public Result<DeliveryAddressVo> getDefaultDeliveryAddress() {
        DeliveryAddressVo deliveryAddressVo = deliveryAddressService.getDefaultDeliveryAddress();
        return Result.success(deliveryAddressVo);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据Id查询收获地址")
    public Result<DeliveryAddressVo> getDeliveryAddressById(@PathVariable Long id) {
        DeliveryAddress deliveryAddress = deliveryAddressService
                .lambdaQuery()
                .select(
                        DeliveryAddress::getId,
                        DeliveryAddress::getDetailAddress,
                        DeliveryAddress::getCity,
                        DeliveryAddress::getIsDefault,
                        DeliveryAddress::getDistrict,
                        DeliveryAddress::getProvince,
                        DeliveryAddress::getReceiverName,
                        DeliveryAddress::getReceiverPhone
                )
                .eq(DeliveryAddress::getId, id)
                .one();
        DeliveryAddressVo deliveryAddressVo = new DeliveryAddressVo();
        BeanUtils.copyProperties(deliveryAddress, deliveryAddressVo);
        return Result.success(deliveryAddressVo);
    }


    @GetMapping("/china/cities")
    @Operation(summary = "获取中国省市区数据")
    public Result<String> getChinaCities() {
        String res = deliveryAddressService.getChinaCities();
        return Result.success(res);
    }

}
